package com.zhoug.ocr.textin;

import com.zhoug.ocr.textin.data.BusinessLicense;
import com.zhoug.ocr.textin.data.BusinessResult;
import com.zhoug.ocr.textin.data.FormResult;
import com.zhoug.ocr.textin.data.VatInvoiceResult;
import com.zhoug.ocr.textin.data.bill.BaseBill;
import com.zhoug.ocr.textin.data.bill.BillResult;
import com.zhoug.ocr.textin.data.bill.VatInvoice;
import com.zhoug.ocr.textin.http.TextInHttpClient;
import com.zhoug.ocr.textin.http.core.TextInResponseTransformer;
import com.zhoug.ocr.textin.parser.BillResultParser;
import com.zhoug.ocr.textin.parser.BusinessResultParser;
import com.zhoug.ocr.textin.parser.VatInvoiceResultParser;
import com.zhoug.common.content.Optional;
import com.zhoug.common.framework.rxjava2.SchedulerUtils;
import com.zhoug.common.http.core.ContentType;

import java.io.File;
import java.util.List;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @Author: zhoug
 * @Date: 2023/3/6
 * @Description:
 */
class TextInRepository {

    public Observable<Optional<List<BaseBill>>> recognizeBill(String filePath) {
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse(ContentType.OTCET_STREAM), file);
        return TextInHttpClient.getInstance().getRequestMethods().recognitionBill(TextInConfig.getApiBill(), requestBody)
                .subscribeOn(SchedulerUtils.io())
                .compose(TextInResponseTransformer.handleResult())
                .observeOn(SchedulerUtils.io())
                .map(formResultOptional -> {
                    BillResult result = formResultOptional.get();
//                    LogUtils.d(TAG, "result:"+result);
                    BillResultParser parser = new BillResultParser();
                    return Optional.create(parser.parse(result));
                });
    }

    /**
     * 增值税发票识别
     *
     * @param filePath 发票路径
     */
    public Observable<Optional<VatInvoice>> recognizeVatInvoice(String filePath) {
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse(ContentType.OTCET_STREAM), file);
        return TextInHttpClient.getInstance().getRequestMethods().recognitionVatInvoice(TextInConfig.getApiVatInvoice(), requestBody)
                .subscribeOn(SchedulerUtils.io())
                .compose(TextInResponseTransformer.handleResult())
                .observeOn(SchedulerUtils.io())
                .map(formResultOptional -> {
                    VatInvoiceResult result = formResultOptional.get();
//                    LogUtils.d(TAG, "result:"+result);
                    VatInvoiceResultParser parser = new VatInvoiceResultParser();
                    return Optional.create(parser.parseVatInvoice(result));
                });
    }

    /**
     * 营业执照识别
     *
     * @param filePath
     */

    public Observable<Optional<BusinessLicense>> recognizeBusinessLicense(String filePath) {
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse(ContentType.OTCET_STREAM), file);
        return TextInHttpClient.getInstance().getRequestMethods().recognitionBusinessLicense(TextInConfig.getApiBusinessLicense(), requestBody)
                .subscribeOn(SchedulerUtils.io())
                .compose(TextInResponseTransformer.handleResult())
                .observeOn(SchedulerUtils.io())
                .map(formResultOptional -> {
                    BusinessResult result = formResultOptional.get();
//                    LogUtils.d(TAG, "result:"+result);
                    BusinessResultParser parser = new BusinessResultParser();
                    return Optional.create(parser.parseBusinessLicense(result));
                });


    }


    /**
     * 通用表格识别
     *
     * @param filePath
     * @return
     */
    public Observable<Optional<FormResult>> recognitionForm(String filePath) {
        File file = new File(filePath);
        RequestBody requestBody = RequestBody.create(MediaType.parse(ContentType.OTCET_STREAM), file);
        Observable<Optional<FormResult>> table = null;
        if (TextInConfig.isLocalApi()) {
            table = TextInHttpClient.getInstance().getRequestMethods().recognitionFormLocal(TextInConfig.getApiTable(), "table_only", requestBody)
                    .subscribeOn(SchedulerUtils.io())
                    .map(Optional::create);
        } else {
            table = TextInHttpClient.getInstance().getRequestMethods().recognitionForm(TextInConfig.getApiTable(), "table_only", requestBody)
                    .subscribeOn(SchedulerUtils.io())
                    .compose(TextInResponseTransformer.handleResult());
        }
        return table;
    }


}
